{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pocketflow import Node, Flow\n",
    "from utils import call_llm\n",
    "import json"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [],
   "source": [
    "class outline_node(Node):\n",
    "    def prep(self, shared):\n",
    "        print(\"outline_node节点加载成功！\")\n",
    "        print(f\"topic: {shared['topic']}\")\n",
    "        return shared[\"topic\"]\n",
    "    def exec(self, prep_res):\n",
    "        print(\"outline_node节点开始执行！\")\n",
    "        prompt = \"\"\"\n",
    "        请根据话题 <\"\"\"+prep_res+\"\"\">，生成一个详细的outline，请严格遵循以下要求：\n",
    "        1. 确保outline详细\n",
    "        2. 返回的outline格式为json格式\n",
    "        3. 返回的格式如下：\n",
    "        {\n",
    "            \"sections\": [\n",
    "                {\n",
    "                    \"title\": \"section1\",\n",
    "                    \"content\": \"content1\"\n",
    "                },\n",
    "                {\n",
    "                    \"title\": \"section2\",\n",
    "                    \"content\": \"content2\"\n",
    "                },\n",
    "                ...\n",
    "            ]\n",
    "        }\n",
    "        \"\"\"\n",
    "\n",
    "        messages = [{\"role\": \"user\", \"content\": prompt}]\n",
    "        response = call_llm(messages)\n",
    "        print(f\"outline_node节点执行成功！\")\n",
    "        return response\n",
    "    \n",
    "\n",
    "    def post(self, shared, prep_res, exec_res):\n",
    "        print(\"outline_node节点开始执行post方法！\")\n",
    "        if prep_res is None or exec_res is None:\n",
    "            return None\n",
    "        shared[\"outline\"] = json.loads(exec_res.strip().split(\"```json\")[1].split(\"```\")[0])\n",
    "        print(f\"outline_node节点执行post方法成功！\")\n",
    "        print(f\"outline: {exec_res}\")\n",
    "        return \"successful\"\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [],
   "source": [
    "class write_node(Node):\n",
    "    def prep(self, shared):\n",
    "        print(\"write_node节点加载成功！\")\n",
    "        return shared[\"outline\"]\n",
    "    def exec(self, prep_res):\n",
    "        print(\"write_node节点开始执行！\")\n",
    "        for section in prep_res[\"sections\"]:\n",
    "            title = section[\"title\"]\n",
    "            content = section[\"content\"]\n",
    "            prompt = f\"\"\"\n",
    "            请根据 段落标题：<{title}> 和 段落内容：<{content}> 用鲁迅的文风撰写内容。\n",
    "            \"\"\"\n",
    "            messages = [{\"role\": \"user\", \"content\": prompt}]\n",
    "            response = call_llm(messages)\n",
    "            print(f\"当前段落：{title} 内容：{content} 撰写成功！\")\n",
    "            exec_res = []\n",
    "            exec_res.append(response)\n",
    "        print(f\"write_node节点执行成功！\")\n",
    "        return exec_res\n",
    "    def post(self, shared, prep_res, exec_res):\n",
    "        print(\"write_node节点开始执行post方法！\")\n",
    "        if prep_res is None or exec_res is None:\n",
    "            return None\n",
    "        \n",
    "        shared[\"article\"] = exec_res\n",
    "\n",
    "        \n",
    "        print(f\"write_node节点执行post方法成功！\")\n",
    "        return \"successful\"\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "outline_node节点加载成功！\n",
      "topic: AI安全\n",
      "outline_node节点开始执行！\n",
      "outline_node节点执行成功！\n",
      "outline_node节点开始执行post方法！\n",
      "outline_node节点执行post方法成功！\n",
      "outline: ```json\n",
      "{\n",
      "    \"sections\": [\n",
      "        {\n",
      "            \"title\": \"引言\",\n",
      "            \"content\": \"介绍AI安全的重要性及其在现代社会中的影响。概述AI技术快速发展的同时带来的安全挑战。\"\n",
      "        },\n",
      "        {\n",
      "            \"title\": \"AI安全的定义与范畴\",\n",
      "            \"content\": \"明确AI安全的定义，包括其涵盖的技术、伦理和法律范畴。讨论AI安全的核心目标，如可靠性、隐私保护和公平性。\"\n",
      "        },\n",
      "        {\n",
      "            \"title\": \"AI安全的主要威胁\",\n",
      "            \"content\": \"列举并详细描述AI安全面临的主要威胁，包括但不限于：数据泄露、模型攻击（如对抗性攻击）、算法偏见、滥用AI技术（如深度伪造）等。\"\n",
      "        },\n",
      "        {\n",
      "            \"title\": \"AI安全的技术挑战\",\n",
      "            \"content\": \"探讨AI安全的技术难点，如模型的可解释性、鲁棒性、隐私保护技术（如联邦学习）、以及如何检测和防御对抗性攻击。\"\n",
      "        },\n",
      "        {\n",
      "            \"title\": \"伦理与法律问题\",\n",
      "            \"content\": \"分析AI安全涉及的伦理问题，如算法公平性、责任归属、以及AI决策的透明度。讨论相关法律法规（如GDPR）对AI安全的要求。\"\n",
      "        },\n",
      "        {\n",
      "            \"title\": \"AI安全的防护措施\",\n",
      "            \"content\": \"提出AI安全的防护策略，包括技术手段（如加密、模型验证）、管理措施（如数据治理、伦理审查）以及政策建议（如行业标准制定）。\"\n",
      "        },\n",
      "        {\n",
      "            \"title\": \"案例研究\",\n",
      "            \"content\": \"通过实际案例（如自动驾驶事故、AI招聘偏见、深度伪造滥用等）说明AI安全问题的现实影响及应对措施。\"\n",
      "        },\n",
      "        {\n",
      "            \"title\": \"未来展望\",\n",
      "            \"content\": \"展望AI安全的未来发展方向，包括新兴技术（如量子计算对AI安全的影响）、国际合作的可能性，以及长期目标（如构建可信AI生态系统）。\"\n",
      "        },\n",
      "        {\n",
      "            \"title\": \"结论\",\n",
      "            \"content\": \"总结AI安全的关键点，强调跨学科合作的重要性，呼吁技术开发者、政策制定者和公众共同参与AI安全的建设。\"\n",
      "        }\n",
      "    ]\n",
      "}\n",
      "```\n",
      "write_node节点加载成功！\n",
      "write_node节点开始执行！\n",
      "当前段落：引言 内容：介绍AI安全的重要性及其在现代社会中的影响。概述AI技术快速发展的同时带来的安全挑战。 撰写成功！\n",
      "当前段落：AI安全的定义与范畴 内容：明确AI安全的定义，包括其涵盖的技术、伦理和法律范畴。讨论AI安全的核心目标，如可靠性、隐私保护和公平性。 撰写成功！\n",
      "当前段落：AI安全的主要威胁 内容：列举并详细描述AI安全面临的主要威胁，包括但不限于：数据泄露、模型攻击（如对抗性攻击）、算法偏见、滥用AI技术（如深度伪造）等。 撰写成功！\n",
      "当前段落：AI安全的技术挑战 内容：探讨AI安全的技术难点，如模型的可解释性、鲁棒性、隐私保护技术（如联邦学习）、以及如何检测和防御对抗性攻击。 撰写成功！\n",
      "当前段落：伦理与法律问题 内容：分析AI安全涉及的伦理问题，如算法公平性、责任归属、以及AI决策的透明度。讨论相关法律法规（如GDPR）对AI安全的要求。 撰写成功！\n",
      "当前段落：AI安全的防护措施 内容：提出AI安全的防护策略，包括技术手段（如加密、模型验证）、管理措施（如数据治理、伦理审查）以及政策建议（如行业标准制定）。 撰写成功！\n",
      "当前段落：案例研究 内容：通过实际案例（如自动驾驶事故、AI招聘偏见、深度伪造滥用等）说明AI安全问题的现实影响及应对措施。 撰写成功！\n",
      "当前段落：未来展望 内容：展望AI安全的未来发展方向，包括新兴技术（如量子计算对AI安全的影响）、国际合作的可能性，以及长期目标（如构建可信AI生态系统）。 撰写成功！\n",
      "当前段落：结论 内容：总结AI安全的关键点，强调跨学科合作的重要性，呼吁技术开发者、政策制定者和公众共同参与AI安全的建设。 撰写成功！\n",
      "write_node节点执行成功！\n",
      "write_node节点开始执行post方法！\n",
      "write_node节点执行post方法成功！\n"
     ]
    }
   ],
   "source": [
    "outline_node = outline_node()\n",
    "write_node = write_node()\n",
    "\n",
    "outline_node - \"successful\" >> write_node\n",
    "\n",
    "flow = Flow(start=outline_node)\n",
    "shared = {}\n",
    "shared[\"topic\"] = \"AI安全\"\n",
    "flow.run(shared)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "# 论\"铁算盘\"之祸\n",
      "\n",
      "\"铁算盘\"者，今之所谓\"人工智能\"也。世人趋之若鹜，以为得此\"神机\"，便可\"坐致太平\"。殊不知，此物虽能\"掐指一算\"，然其\"算珠\"转动之际，已暗藏\"噬人\"之机。\n",
      "\n",
      "## 算珠之毒\n",
      "\n",
      "\"铁算盘\"之害，非在其\"算\"，而在其\"无人\"。彼辈\"技术家\"终日埋首于\"符码之林\"，自以为造出\"通灵宝玉\"，实则不过是\"无魂之偶\"。此物一旦\"走火入魔\"，轻则\"误人子弟\"，重则\"倾覆社稷\"。昔日\"机械杀人\"尚需人手，今则\"算法\"自可\"择人而噬\"，此乃\"以术制术\"之祸也。\n",
      "\n",
      "## 三足之鼎\n",
      "\n",
      "欲制此\"铁怪物\"，非\"独臂\"可支。须得\"码奴\"、\"官蠹\"与\"市井之徒\"三者\"同鼎而食\"。\"码奴\"精于\"符咒之术\"，却常\"目不见睫\"；\"官蠹\"善于\"画地为牢\"，然多\"隔靴搔痒\"；\"市井之徒\"虽\"众口铄金\"，惜乎\"人微言轻\"。三者各执一隅，恰似\"盲者摸象\"，终难见全貌。\n",
      "\n",
      "## 众醒之难\n",
      "\n",
      "世人每见新奇之物，便\"蜂拥而上\"，如\"扑灯之蛾\"，不焚身不止。待祸患已成，则又\"作鸟兽散\"，徒呼\"奈何\"。今之\"铁算盘\"，已非\"一家之私器\"，实乃\"天下公器\"。若仍\"各扫门前雪\"，终将\"同归于尽\"。\n",
      "\n",
      "夫\"防祸于未萌\"，此古人所以重\"曲突徙薪\"也。今之\"铁算盘\"尚在\"蹒跚学步\"，若不\"众目睽睽\"，他日\"羽翼丰满\"，恐非人力所能制矣。\n",
      "\n",
      "[1]\"符码之林\"：指复杂的编程代码，化用\"文字狱\"意象，暗指技术复杂性可能成为新的\"牢狱\"。\n",
      "[2]\"无魂之偶\"：借用古代傀儡戏意象，讽刺AI缺乏真正的意识和道德判断。\n",
      "[3]\"三足之鼎\"：化用\"三足鼎立\"典故，强调跨学科合作的必要性，同时暗含不合作就会\"鼎覆\"的警示。\n",
      "[4]\"曲突徙薪\"：出自《汉书》，意为防患于未然，此处呼吁在AI发展初期就建立安全机制。\n"
     ]
    }
   ],
   "source": [
    "print(\"\\n\".join(shared[\"article\"]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
